/****************************************************************************

 Copyright (C) 2009 Jean Pierre Charalambos. All rights reserved.

 Jean Pierre Charalambos (http://dis.unal.edu.co/profesores/pierre/) is an
 Affiliate Associate Professor of the Faculty of Engineering at National
 University of Colombia (http://www.unal.edu.co/).

 This file is part of maku version 0.2.

 http://code.google.com/p/maku/
 email: jpcharalambosh@unal.edu.co

 This file may be used under the terms of the GNU General Public License
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the COPYING file included in the packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include "simplescene.h"

#include <QGraphicsLineItem>
#include <QGraphicsSceneMouseEvent>
#include <QAction>
#include <QColorDialog>

//QGraphicsScene se encarga del manejo de memoria ;-)
SimpleScene::~SimpleScene() {
}

void SimpleScene::createActions() {
    lineColorAction = new QAction ( tr ( "&Line color" ), this );
    lineColorAction->setShortcut ( tr ( "Ctrl+C" ) );
    lineColorAction->setStatusTip ( tr ( "Set line color" ) );
    connect ( lineColorAction, SIGNAL ( triggered() ), this, SLOT ( setLineColor() ) );
}

void SimpleScene::setLineColor() {
    QColor color = QColorDialog::getColor ( lineColor, 0 );
    if ( color.isValid() )
        lineColor = color;
}

void SimpleScene::mousePressEvent ( QGraphicsSceneMouseEvent *mouseEvent ) {
    if ( mouseEvent->button() != Qt::LeftButton )
        return;
    //ojo! dibujo interactivo de la linea: la siguiente es la linea que se dibuja mientras se arrastra el mouse, es diferente de la linea definitiva, definida implicitamente en el metodo mouseReleaseEvent
    line = new QGraphicsLineItem ( QLineF ( mouseEvent->scenePos(), mouseEvent->scenePos() ) );
    line->setPen ( QPen ( lineColor ) );
    addItem ( line );
    QGraphicsScene::mousePressEvent ( mouseEvent );
    initPos = mouseEvent->scenePos();
}

void SimpleScene::mouseMoveEvent ( QGraphicsSceneMouseEvent *mouseEvent ) {
    if ( mouseGrabberItem() !=0 ) {
        QGraphicsScene::mouseMoveEvent ( mouseEvent );//mueve el objeto seleccionado
    }
    //dibujo interactivo de la linea...
    else if ( line != 0 ) {
        QLineF newLine ( line->line().p1(), mouseEvent->scenePos() );
        line->setLine ( newLine );
    }
}

void SimpleScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent *mouseEvent ) {
    if ( ( mouseGrabberItem() == 0 ) && ( line != 0 ) ) {
        removeItem ( line );
        delete line;
        QGraphicsLineItem *item;
        item = new QGraphicsLineItem ( QLineF ( initPos, mouseEvent->scenePos() ) );
        item->setPen ( QPen ( lineColor ) );
        item->setFlag ( QGraphicsItem::ItemIsMovable, true );
        item->setFlag ( QGraphicsItem::ItemIsSelectable, true );
        addItem ( item );
        //la linea propia podría definirse asi (haría falta implementar UNLine::paint):
        // /**
        UNLine *unLine;
        unLine = new UNLine ( QLineF ( initPos, mouseEvent->scenePos() ) );
        unLine->setFlag ( QGraphicsItem::ItemIsMovable, true );
        unLine->setFlag ( QGraphicsItem::ItemIsSelectable, true );
        addItem ( unLine );
        // */
    }
    line = 0;
}
